package com.entrance.menuadapter;

import android.app.Fragment;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.entrance.MainActivity;
import com.landmesure.bluetoothchat.R;
import com.widget.SelectCallBack;

import java.util.ArrayList;

public class MenuFragment extends Fragment {
    private Context mContext;
    private ArrayList<GroupType> mGroupTypes = new ArrayList<>();
    private SparseArray<Fragment> mFragmentSparseArray = new SparseArray<>();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();

        //初始化组信息
        initGroup();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_menu_fragment, container, false);

        ExpandableListView listView = view.findViewById(R.id.main_menu_view);
        MenuAdapter menuAdapter = new MenuAdapter(mContext, inflater, mGroupTypes);
        final SelectCallBack selectCallBack = menuAdapter;
        listView.setAdapter(menuAdapter);
        for (int i = 0; i < mGroupTypes.size(); i++) {
            listView.expandGroup(i);
        }
        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                int itemID = createItemId(groupPosition, childPosition);
                selectCallBack.setSelectItem(itemID);

                //避归处理
                /*if (itemID == createItemId(0, 0)) {
                    Intent intent = new Intent(mContext, BluetoothChat.class);
                    mContext.startActivity(intent);
                    return true;
                }*/

                ((MainActivity) mContext).mFragmentUpdate
                        .updateFragment(R.id.main_layout, mFragmentSparseArray.get(itemID));
                return true;
            }
        });
        return view;
    }

    private void initGroup() {
        Resources res = getActivity().getResources();
        String[] groups = res.getStringArray(R.array.menu_group_array);
        for (String name : groups) {
            mGroupTypes.add(new GroupType(name));
        }

        String[][] childs = new String[][] {
                res.getStringArray(R.array.menu_device_list),
                res.getStringArray(R.array.menu_sensor_list)

        };
        for (int i = 0; i < mGroupTypes.size(); i++) {
            initChild(mGroupTypes.get(i), i, childs[i]);
        }
    }

    private void initChild(GroupType group, int groupPosition, String[] childs) {
        for (int i = 0; i < childs.length; i++) {
            int itemID = createItemId(groupPosition, i);
            group.addItem(itemID, childs[i]);
            initSAFragment(groupPosition, itemID, i);
        }
    }

    private void initSAFragment(int groupPosition, int itemID, int childPosition) {
        mFragmentSparseArray.put(itemID,
                ((MainActivity) mContext).mFragments[groupPosition][childPosition]);
    }

    public static int createItemId(int groupPosition, int childPosition) {
        return (groupPosition << 8) + childPosition;
    }
}
